package com.ruoyi.community.controller.wxApi;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.community.domain.Region;
import com.ruoyi.community.service.IRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 行政区域Controller
 *
 * @author wj
 * @date 2022-02-22
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/wx/community/region")
@Api(tags = {"行政区域"})
public class WxRegionController extends BaseController
{
    @Autowired
    private IRegionService regionService;

    /**
     * 查询行政区域列表
     */
    @ApiOperation(value = "查询行政区域列表")
    @GetMapping("/list")
    public AjaxResult list(Region region)
    {
        List<Region> list = regionService.selectRegionList(region);
        return AjaxResult.success(list);
    }

    /**
     * 导出行政区域列表
     */
    @ApiOperation(value = "导出行政区域列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Region region)
    {
        List<Region> list = regionService.selectRegionList(region);
        ExcelUtil<Region> util = new ExcelUtil<Region>(Region.class);
        util.exportExcel(response, list, "行政区域数据");
    }

    /**
     * 获取行政区域详细信息
     */
    @ApiOperation(value = "获取行政区域详细信息")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo( Long id)
    {
        return AjaxResult.success(regionService.selectRegionById(id));
    }

    /**
     * 新增行政区域
     */
    @ApiOperation(value = "新增行政区域")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Region region)
    {
        return toAjax(regionService.insertRegion(region));
    }

    /**
     * 修改行政区域
     */
    @ApiOperation(value = "修改行政区域")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Region region)
    {
        return toAjax(regionService.updateRegion(region));
    }

    /**
     * 删除行政区域
     */
    @ApiOperation(value = "删除行政区域")
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        Long[] ids= {id};
        return toAjax(regionService.deleteRegionByIds(ids));
    }
}
